package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdclphbsjBaDetailBBo;
import com.ruoyi.report_wjjhl.domain.vo.WdclphbsjBaDetailBVo;
import com.ruoyi.report_wjjhl.domain.WdclphbsjBaDetailB;
import com.ruoyi.report_wjjhl.mapper.WdclphbsjBaDetailBMapper;
import com.ruoyi.report_wjjhl.service.IWdclphbsjBaDetailBService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 无机结合料稳定材料配合比设计试验报告详细bService业务层处理
 *
 * @author zyb
 * @date 2024-08-21
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdclphbsjBaDetailBServiceImpl implements IWdclphbsjBaDetailBService {

    private final WdclphbsjBaDetailBMapper baseMapper;

    /**
     * 查询无机结合料稳定材料配合比设计试验报告详细b
     */
    @Override
    public WdclphbsjBaDetailBVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询无机结合料稳定材料配合比设计试验报告详细b列表
     */
    @Override
    public TableDataInfo<WdclphbsjBaDetailBVo> queryPageList(WdclphbsjBaDetailBBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdclphbsjBaDetailB> lqw = buildQueryWrapper(bo);
        Page<WdclphbsjBaDetailBVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询无机结合料稳定材料配合比设计试验报告详细b列表
     */
    @Override
    public List<WdclphbsjBaDetailBVo> queryList(WdclphbsjBaDetailBBo bo) {
        LambdaQueryWrapper<WdclphbsjBaDetailB> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdclphbsjBaDetailB> buildQueryWrapper(WdclphbsjBaDetailBBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdclphbsjBaDetailB> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, WdclphbsjBaDetailB::getExcelId, bo.getExcelId());
        lqw.eq(bo.getQualId() != null, WdclphbsjBaDetailB::getQualId, bo.getQualId());
        lqw.eq(StringUtils.isNotBlank(bo.getSymbolValue()), WdclphbsjBaDetailB::getSymbolValue, bo.getSymbolValue());
        lqw.eq(bo.getNum1() != null, WdclphbsjBaDetailB::getNum1, bo.getNum1());
        lqw.eq(bo.getNum2() != null, WdclphbsjBaDetailB::getNum2, bo.getNum2());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszb()), WdclphbsjBaDetailB::getJiszb, bo.getJiszb());
        lqw.eq(bo.getJiancjg() != null, WdclphbsjBaDetailB::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), WdclphbsjBaDetailB::getJiegpd, bo.getJiegpd());
        lqw.eq(bo.getShuinNum() != null, WdclphbsjBaDetailB::getShuinNum, bo.getShuinNum());
        lqw.eq(bo.getJilNum() != null, WdclphbsjBaDetailB::getJilNum, bo.getJilNum());
        lqw.eq(bo.getZuijhsl() != null, WdclphbsjBaDetailB::getZuijhsl, bo.getZuijhsl());
        lqw.eq(bo.getZuidgmd() != null, WdclphbsjBaDetailB::getZuidgmd, bo.getZuidgmd());
        lqw.eq(bo.getShejz() != null, WdclphbsjBaDetailB::getShejz, bo.getShejz());
        lqw.eq(bo.getPingjz() != null, WdclphbsjBaDetailB::getPingjz, bo.getPingjz());
        lqw.eq(bo.getBiaozc() != null, WdclphbsjBaDetailB::getBiaozc, bo.getBiaozc());
        lqw.eq(bo.getPiancxs() != null, WdclphbsjBaDetailB::getPiancxs, bo.getPiancxs());
        lqw.eq(StringUtils.isNotBlank(bo.getWucxkyjg()), WdclphbsjBaDetailB::getWucxkyjg, bo.getWucxkyjg());
        return lqw;
    }

    /**
     * 新增无机结合料稳定材料配合比设计试验报告详细b
     */
    @Override
    public Boolean insertByBo(WdclphbsjBaDetailBBo bo) {
        WdclphbsjBaDetailB add = BeanUtil.toBean(bo, WdclphbsjBaDetailB.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改无机结合料稳定材料配合比设计试验报告详细b
     */
    @Override
    public Boolean updateByBo(WdclphbsjBaDetailBBo bo) {
        WdclphbsjBaDetailB update = BeanUtil.toBean(bo, WdclphbsjBaDetailB.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdclphbsjBaDetailB entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除无机结合料稳定材料配合比设计试验报告详细b
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
